﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_ReflectDamage : Buff
    {
        public Spell_ReflectDamage() { }


        public int reflectDamageReturnRemainder(CreatureBase attackingCreatureBase, int damageAmount)
        {
            int percentageToReflect = 5 + (CastingCreatureLevel / 2);

            int dmg = System.Convert.ToInt32(damageAmount * (percentageToReflect / 100.00)); //SkillManager.AdjustForCombat(CastingCreatureLevel / 2, 1, CastingCreatureWis / 3);
            if (dmg > CastingCreatureLevel * 10)
                dmg = CastingCreatureLevel * 10;

            attackingCreatureBase.InflictDamage(dmg, CastingCharacterID);
            attackingCreatureBase.Update_Character();

            if (attackingCreatureBase is Character)
            {
                Character myCharacter = (Character)attackingCreatureBase;

                Message msg = new Message();
                msg.CreatureID = myCharacter.CreatureID;
                msg.Type = "spelldamaged";
                msg.Text = TargetCreatureBase.Name + " reflects! (" + dmg.ToString() + ")";

                myCharacter.AddMessage(msg);
            }
            if (TargetCreatureBase is Character && TargetCreatureBase != attackingCreatureBase)
            {
                Character targetCharacter = (Character)TargetCreatureBase;

                Message msg = new Message();
                msg.CreatureID = targetCharacter.CreatureID;
                msg.Type = "spelldamage";
                msg.Text = "You reflect " + attackingCreatureBase.Name + "! (" + dmg.ToString() + ")";

                targetCharacter.AddMessage(msg);
            }

            if (attackingCreatureBase.HP < 1)
            {
                if (!(attackingCreatureBase is Character))
                    attackingCreatureBase.currentMapItem.CreaturesBases.Remove(attackingCreatureBase);
            }

            List<Character> updateCharacters = attackingCreatureBase.currentMapItem.getCharactersWithin4();
            foreach (Character character in updateCharacters)
            {
                if (attackingCreatureBase.HP < 1)
                    character.AddMapItem(attackingCreatureBase.currentMapItem);
                else
                    character.UpdateCreatureBaseHp(attackingCreatureBase);
            }

            return damageAmount - dmg;
        }

        public override void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            minutesLeft = timeRemaining;
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if (buff.spellIcon.Name == base.spellIcon.Name && buff.SpellID != SpellID)
                {
                    buff.RemoveSpell(false, false);
                }
            }

            spellTimer.Start();
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;

            minutesLeft = CastingCreatureWis / 7;

            TargetCreatureBase.buffManager.UpdateBuffs();

            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 8)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                        {
                            Character character = (Character)creaturebase;
                            character.PlaySound("BuffCast", character.MapX, character.MapY);
                        }
                    }
                }
            }

            //Start Spell Timer
            spellTimer.Start();
        }
    }
}
